<?php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserLogin;
use App\Http\Requests\UserRegister;
use App\Models\EmailSend;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{

    public function test()
    {
        return response()->json(array('error' => 1,'env' => app()->environment()));
    }

    /**
     * 用户注册界面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function showRegister()
    {
        return view('web.user.showRegister');
    }
    /**
     * 用户注册
     * @param UserRegister $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function register(UserRegister $request)
    {
        $validated = $request->validated();
        //验证成功进行用户注册，并返回token
        $userEmail =  User::create(
            array(
                'name'=>$validated['name'],
                'email' => $validated['email'],
                'password' => Hash::make($validated['password']),
                'created_at' => time()
            )
        );
        $userInfo = array('email' =>  '','token' => '','back_url' => '');
        $result = app('resultJson');
        if(!empty($userEmail->password)&&(Hash::check($validated['password'], $userEmail->password)))
        {
            $userEmail->tokens()->delete();
            $userInfo['email'] = $userEmail->email;
            $userInfo['token'] = $userEmail->createToken($userEmail->email)->plainTextToken;
            $userInfo['back_url'] = $request->get('back_url', route('home'));
        }
        return response()->json($result->setData($userInfo));
    }

    /**
     * 邮箱验证
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getVerification(Request $request)
    {
        $email = $request->get('email');
        $userInfo = array('email' =>  '');
        $result = app('resultJson');
        if(!empty($email))
        {
            //生产一个随机的数字验证码
            $verifyCode = rand(1000,9999);
            $id = DB::table('email_verify')->insertGetId([
                'email' => $email,
                'verify_code' => $verifyCode,
                'created_at' => time()
            ]);
            if(!empty($id))
            {
                $emailSend = new EmailSend();
                $userInfo['info'] = $emailSend->sendVerify($email,'邮箱验证','欢迎使用,你的验证码是:'.$verifyCode.'有效时间20分钟;');
                $userInfo['email'] = $email;
            }
        }
        return response()->json($result->setData($userInfo));
    }
    /**
     * 用户登录界面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function showLogin()
    {
        return view('web.user.showLogin');
    }
    /**
     * 登录认证
     * @param UserLogin $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function login(UserLogin $request)
    {
        $validated = $request->validated();
        $result = app('resultJson');
        $userInfo = array('email' =>  '','token' => '','back_url' => '');
        $userEmail = User::where([
            ['email' ,$validated['email']]
        ])->get(['id','email','password'])->first();
        if(empty($userEmail['email']))
        {
            $result->setMessage('register');
            $userInfo['back_url'] = $request->get('back_url',route('user::showRegister'));
        }
        else
        {
            if(Hash::check($validated['password'], $userEmail['password']))
            {
                $userEmail->tokens()->delete();
                $userInfo['email'] = $userEmail['email'];
                $userInfo['token'] = $userEmail->createToken($userEmail['email'])->plainTextToken;
                $userInfo['back_url'] = $request->get('back_url', route('home'));
            }
            else
            {
                $result->setMessage('UserLogin');
                $userInfo['password'] = '密码不正确';
            }
        }
        return response()->json($result->setData($userInfo));
    }

    /**
     * 网站政策界面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function policy()
    {
        return view('web.policy');
    }
}